package com.minyifang.wcspf.work.ui.user.activities;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseActivity;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.utils.AppUtils;
import com.minyifang.wcspf.common.utils.CacheUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.common.widgets.CustomDialog;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.common.MyWebViewActivity;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.home.activities.MainActivity;
import com.minyifang.wcspf.work.ui.user.presenter.SettingPresenter;
import com.minyifang.wcspf.work.ui.user.view.SettingView;

/**
 * 设置
 */
public class SettingActivity extends BaseMvpActivity<SettingView, SettingPresenter> implements SettingView {


    @BindView(R.id.ll_need_login)
    LinearLayout mLlNeedLogin;

    @BindView(R.id.ll_need_login2)
    LinearLayout mLlNeedLogin2;

    @BindView(R.id.tv_cache_size)
    TextView mTvCacheSize;

    @BindView(R.id.tv_version)
    TextView mTvVersion;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        //未登录隐藏选项
        if (!UserManager.getInstance().isLogin()) {
            mLlNeedLogin.setVisibility(View.GONE);
            mLlNeedLogin2.setVisibility(View.GONE);
        }
        mTvCacheSize.setText(CacheUtils.getTotalCacheSize(this));

        mTvVersion.setText(AppUtils.getVersionName(this));
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter();
    }


    @Override
    protected void initData() {
//        requestTranslucentStatusBar(Color.TRANSPARENT, true);
    }

    /**
     * 修改密码
     *
     * @param view
     */
    public void changePwd(View view) {
        startActivity(new Intent(this, UpdatePwdActivity.class));
    }

    /**
     * 清除缓存
     *
     * @param view
     */
    public void clearCache(View view) {
        new CustomDialog.Builder(this)
                .setTitle("清理缓存")
                .setContent("确认清除缓存？")
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        Toast.makeText(SettingActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
                        CacheUtils.clearAllCache(SettingActivity.this);
                        mTvCacheSize.setText(CacheUtils.getTotalCacheSize(SettingActivity.this));
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }

    /**
     * 关于我们
     *
     * @param view
     */
    public void aboutUs(View view) {
        mPresenter.getAboutUsData();
    }

    /**
     * 版本介绍
     *
     * @param view
     */
    public void versionDesc(View view) {
        mPresenter.getVersionIntroduction();
    }

    /**
     * 登出账号
     *
     * @param view
     */
    public void logOut(View view) {
        //未登录直接 return
        if (!UserManager.getInstance().isLogin())
            return;

        new CustomDialog.Builder(this)
                .setTitle("退出登录")
                .setContent("确认退出当前账号？")
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        UserManager.getInstance().clearLoginData();
                        closeAllActivities();
                        gotoMain();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();
    }

    private void gotoMain() {
        startActivity(new Intent(SettingActivity.this, MainActivity.class));
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void showAboutUsData(HttpRespond<String> respond) {
        if (respond.result == 1) {
            LogUtil.e("关于我们--" + respond.data);
            startActivity(MyWebViewActivity.getIntent(this, "关于我们", respond.data));
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showVersionIntroductionData(HttpRespond<String> respond) {
        if (respond.result == 1) {
            startActivity(MyWebViewActivity.getIntent(this, "版本介绍", respond.data));
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }
}
